#include "StdAfx.h"
#include "ShareM.h"

CShareMem::CShareMem()
{
  hFile=(HANDLE)NULL;
  hMap=NULL;
  hName=(LPCTSTR)"";
//  pMapView=void *;
  type=0;
}

CShareMem::~CShareMem()
{
  if(hFile != NULL || hFile != (HANDLE)-1)::CloseHandle(hFile);
  if(hMap)::CloseHandle(hMap);
}

bool CShareMem::CreateFile(LPCTSTR FileName)
{
  this->hFile=::CreateFile(FileName,
						   GENERIC_READ|GENERIC_WRITE,
						   FILE_SHARE_READ|FILE_SHARE_WRITE,
						   NULL,
						   CREATE_ALWAYS,
						   FILE_ATTRIBUTE_NORMAL,
						   NULL);
  if(this->hFile == (HANDLE)-1){
	return false;
  }
  return true;
}

bool CShareMem::CreateFileMap(LPCTSTR pszName,int MapsizeH,int MapsizeL)
{
/*  HANDLE phd;
  if(hFile != NULL && hFile != (HANDLE)-1){
	phd=this->hFile;
  }else{
	char str[8]="newfile";
	str[7]='\0';
	if(!this->CreateFile((PCTSTR)str))return false;
	phd=this->hFile;
  }//*/
  if(this->server){
    this->hMap=::CreateFileMapping(INVALID_HANDLE_VALUE,
	                             NULL,
								 PAGE_READWRITE,
								 MapsizeH,
								 MapsizeL,
								 pszName);
  }else{
	this->hMap=::OpenFileMapping(FILE_MAP_ALL_ACCESS,
	                             FALSE,
							     pszName);
  }

  if(this->hMap==NULL){
  //  if(phd)::CloseHandle(phd);
  
	return false;
  }

  return true;
}

bool CShareMem::MapViewofFile(int type)
{
  pMapView=::MapViewOfFile(this->hMap,
	FILE_MAP_WRITE,
	0,
	0,
	0);

  if(this->pMapView==NULL)return false;

  return true;
}

bool CShareMem::UnmapViewofFile(void)
{
  return true;
}
